import axios from "axios";
import dayjs from "dayjs";
import useFlowStore from "@/store/flow.js";

const flowStore = useFlowStore();

export default function ({ start, end }, writeStore = true) {
  flowStore.replyData = null;
  let uri = `api/flow_ntfm_get.php?&tag=1&begTime=${start}&endTime=${end} `;
  return new Promise((resolve, reject) => {
    axios
      .get(uri)
      .then((response) => {
        if (writeStore) {
          flowStore.rawData = response.data.rawData;
          flowStore.begTime = response.data.begTime;
          !flowStore.tags && response.data.tags
            ? (flowStore.tags = response.data.tags)
            : null;
          flowStore.version = -1;
          flowStore.reference = 0;
          flowStore.freshness = 0;
        }

        flowStore.replyData = response.data;
        resolve(response.data);
      })
      .catch((error) => {
        console.error("flow_ntfm_get", error);
        reject(error);
      });
  });
}
